package com.xishu.handler;

import com.xishu.entity.customer.Customer;
import com.xishu.entity.customer.CustomerTicket;
import com.xishu.entity.customer.Voucher;
import com.xishu.entity.shop.DiscountTicket;
import com.xishu.service.CommonService;
import com.xishu.service.DiscountTicketService;
import com.xishu.util.DateUtil;
import com.xishu.util.Tools;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class VouchersHandler implements XSSFSheetXMLHandler.SheetContentsHandler, Tools {

    private static Logger logger = LoggerFactory.getLogger(VouchersHandler.class);
    private CommonService commonService = CommonService.getInstance();
    private Voucher voucher;

    private Long ticketId;

    public void setTicketId(Long ticketId) {
        this.ticketId = ticketId;
    }


    @Override
    public void startRow(int i) {
        if (i > 0) {
            voucher = new Voucher();
        }
    }

    @Override
    public void endRow(int i) {
        if (i > 0) {
            if (System.currentTimeMillis() > getTime(voucher.getExpiredDate()) || voucher.getIsUsed().equals("-1")) {
                return;
            }

            Customer customer = new Customer();
            customer.setMemberId(voucher.getMemberId());
            Customer customerDb = commonService.searchOne(customer);

            if (customerDb == null) {
                logger.info("can't find customer");
                return;
            }


            try {
                assignCustomerTicket(customerDb, ticketId);
            } catch (Exception e) {
                logger.error("e", e);
            }
        }
    }

    @Override
    public void cell(String cellReference, String value, XSSFComment xssfComment) {
        if (voucher != null) {
            String trim = value.trim();
            String pix = cellReference.substring(0, 1);
            switch (pix) {
                case "B":
                    voucher.setCreateDate(trim);
                    break;
                case "C":
                    voucher.setMemberId(trim);
                    break;
                case "D":
                    voucher.setDetail(trim);
                    break;
                case "E":
                    voucher.setExpiredDate(trim);
                    break;
                case "G":
                    voucher.setIsUsed(trim);
                    break;
                default:
                    break;

            }
        }
    }

    private void assignCustomerTicket(Customer cusDb, Long ticketId) throws Exception {
        //根据优惠券名找出对应的优惠券模板
        DiscountTicket discountTicket = new DiscountTicket();
        discountTicket.setId(ticketId);
        discountTicket.setCompanyId(cusDb.getCompanyId());
        DiscountTicket discountTicketDd = commonService.searchOne(discountTicket);

        //有模板则创建
        if (isNotEmpty(discountTicketDd)) {
            //创建优惠券
            DiscountTicket realTicket = DiscountTicketService.getInstance().createRealTicket(discountTicketDd);
            realTicket.setLimitNumber(true);
            realTicket.setAssignByExcel(true);
            realTicket.setStartTime(System.currentTimeMillis());
            realTicket.setEndTime(DateUtil.endTimeInDay(getTime((voucher.getExpiredDate()))));
            commonService.save(realTicket);

            //发放优惠券
            CustomerTicket customerTicket = new CustomerTicket();
            customerTicket.setParentTicketId(realTicket.getParentId());
            customerTicket.setCode(realTicket.getCode());
            customerTicket.setLimitNumber(true);
            customerTicket.setCompanyId(realTicket.getCompanyId());
            customerTicket.setCustomerId(cusDb.getId());
            customerTicket.setExpiredTime(realTicket.getEndTime());
            customerTicket.setTicketId(realTicket.getId());
            customerTicket.setAssignByExcel(true);
            commonService.createObject(customerTicket);
            logger.info("send ticket to {}", customerTicket.getCustomerId());
        }
    }


    private long getTime(String registerTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(registerTime);
        } catch (ParseException e) {
            logger.info("e", e);
        }
        long time = parse.getTime();
        return time;
    }

}
